package com.yy.util.bitmap;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;

import com.squareup.okhttp.internal.DiskLruCache;
import com.yy.util.common.MD5;
import com.yy.util.http.IOHelper;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created by Administrator on 2015/5/6.
 */
public class BitmapDiskLruCache {

    private Context context;
    private static DiskLruCache diskLruCache;
    private static BitmapDiskLruCache bitmapDiskLruCache;
    private final static int DISK_CACHE_SIZE = 1024 * 1024 * 10;

    private BitmapDiskLruCache(Context context) {
        this.context = context;
    }

    public static BitmapDiskLruCache open(Context context, File cacheDir, int versionCode, int maxCacheSize) {
        try {
            diskLruCache = DiskLruCache.open(cacheDir, versionCode, 1, maxCacheSize);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(bitmapDiskLruCache == null) {
           bitmapDiskLruCache = new BitmapDiskLruCache(context);
        }
        return bitmapDiskLruCache;
    }

    public File getDiskCacheDir(String uniqueName) {
        final String cachePath = Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
                || !Environment.isExternalStorageRemovable() ? context.getExternalCacheDir().getPath() :
                context.getCacheDir().getPath();
        return new File(cachePath + File.separator + uniqueName);
    }

    public Bitmap get(String key) {
        try {
            final DiskLruCache.Snapshot snapshot = diskLruCache.get(MD5.hashKey(key));
            if(snapshot != null) {
                final InputStream inputStream = snapshot.getInputStream(0);
                /*BitmapHelper helper = new BitmapHelper() {
                    @Override
                    public Bitmap decode(BitmapFactory.Options options) {
                        return BitmapFactory.decodeStream(inputStream, null, options);
                    }
                };*/
                final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                return bitmap;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void put(String key, Bitmap value) {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        DiskLruCache.Editor edit = null;
        InputStream inputStream = BitmapHelper.bitmap2InputStream(value);
        try {
            edit = diskLruCache.edit(MD5.hashKey(key));
            OutputStream os = edit.newOutputStream(0);

            bis = new BufferedInputStream(inputStream, 8 * 1024);
            bos = new BufferedOutputStream(os, 8 * 1024);
            int b;
            while ((b = bis.read()) != -1) {
                bos.write(b);
            }
            edit.commit();
        } catch (IOException e) {
            e.printStackTrace();
            if (edit != null) {
                try {
                    edit.abort();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        } finally {
            try {
                if(bis != null) {
                    bis.close();
                }
                if(bos != null) {
                    bos.close();
                }
                diskLruCache.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
